home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM A / PD-ROM A.iso / Programming / Programming Languages / MacOberon / MacOberon (tools) / EdT.Mod (.txt) < prev    next >
Encoding:
Oberon Text  |  1990-10-18  |  4.1 KB  |  106 lines  |  [.Ob./.Ob2]

  1. Syntax10.Scn.Fnt
  2. MODULE EdT; (* Robert Griesemer / Michael Franz, 1.9.90 *)
  3.         Simple Extension of Edit which supports
  4.             - Auto-Indentation
  5.             - Cursor Keys for Caret Movement
  6.             - Indentation Control of Text Selections by use of Cursor Keys
  7.                 (Viewer must be in focus, i.e. contain Caret)
  8.         Once you have compiled this module, you may use the command
  9.                 EdT.Open name
  10.                 EdT.Open ^
  11.         instead of Edit.Open for opening files in the enhanced Editor.
  12.     IMPORT
  13.         Display, Viewers, Texts, TextFrames, MenuViewers, Oberon;
  14.     CONST
  15.         HT = 9X; LF = 0AX; CR = 0DX; Left = 01CX; Right = 01DX;
  16.         Menu = "System.Close  System.Copy  System.Grow  Edit.Store";
  17.     TYPE
  18.         EdTMsg = RECORD(Display.FrameMsg)
  19.             text: Texts.Text;
  20.             beg, end: LONGINT;
  21.             time: LONGINT
  22.         END;
  23.         W: Texts.Writer;
  24.     PROCEDURE BegOfLine(text: Texts.Text; pos: LONGINT): LONGINT;
  25.         VAR r: Texts.Reader; ch: CHAR;
  26.     BEGIN
  27.         LOOP    DEC(pos);
  28.             IF    pos < 0    THEN    RETURN 0    END;
  29.             Texts.OpenReader(r, text, pos); Texts.Read(r, ch);
  30.             IF    ch = CR    THEN    RETURN pos+1    END
  31.         END
  32.     END BegOfLine;
  33.     PROCEDURE Select(text: Texts.Text; beg, end: LONGINT);
  34.         VAR msg: EdTMsg;
  35.     BEGIN    msg.text := text; msg.beg := beg; msg.end := end; msg.time := Oberon.Time(); Viewers.Broadcast(msg)
  36.     END Select;
  37.     PROCEDURE Move(f: TextFrames.Frame; dx: INTEGER);
  38.         VAR text: Texts.Text; beg, end, time, pos: LONGINT; r: Texts.Reader; ch: CHAR;
  39.     BEGIN    Oberon.GetSelection(text, beg, end, time);
  40.         IF    (time >= 0) & (f.text = text)    THEN    beg := BegOfLine(text, beg); pos := beg;    (* move selection *)
  41.             WHILE    pos < end    DO    Texts.OpenReader(r, text, pos); Texts.Read(r, ch);
  42.                 IF    dx < 0    THEN
  43.                     IF    (ch <= " ") & (ch # CR)    THEN    Texts.Delete(text, pos, pos+1); DEC(end)    END
  44.                 ELSE
  45.                     IF    (ch <= " ") & (ch # CR)    THEN    Texts.Write(W, ch)    ELSE    Texts.Write(W, HT)    END; (* first char extension *)
  46.                     Texts.Insert(text, pos, W.buf); INC(end); INC(pos)
  47.                 END;
  48.                 Texts.OpenReader(r, text, pos);
  49.                 REPEAT    Texts.Read(r, ch)    UNTIL    r.eot OR (ch = CR);
  50.                 pos := Texts.Pos(r)
  51.             END;
  52.             Select(text, beg, pos)
  53.         ELSIF    f.car > 0    THEN    pos := f.carloc.pos+dx;    (* move caret *)
  54.             IF    pos < 0    THEN    pos := 0    ELSIF    pos > f.text.len    THEN    pos := f.text.len    END;
  55.             TextFrames.RemoveCaret(f); TextFrames.SetCaret(f, pos)
  56.         END
  57.     END Move;
  58.     PROCEDURE NewLine(f: TextFrames.Frame);
  59.         VAR r: Texts.Reader; n, car: LONGINT; ch: CHAR;
  60.     BEGIN    Texts.Write(W, CR); car := f.carloc.pos+1; Texts.OpenReader(r, f.text, f.carloc.org); Texts.Read(r, ch);
  61.         WHILE    (Texts.Pos(r) <= f.carloc.pos) & (ch <= " ")    DO    Texts.Write(W, ch); INC(car); Texts.Read(r, ch)    END;
  62.         Texts.Insert(f.text, f.carloc.pos, W.buf); TextFrames.SetCaret(f, car)
  63.     END NewLine;
  64.     PROCEDURE Handle*(F: Display.Frame; VAR msg: Display.FrameMsg);
  65.     BEGIN
  66.         WITH    F: TextFrames.Frame    DO
  67.             IF    msg IS Oberon.InputMsg    THEN
  68.                 WITH    msg: Oberon.InputMsg    DO
  69.                     IF    msg.id = Oberon.consume    THEN
  70.                         IF    msg.ch = Left    THEN    Move(F, -1)
  71.                         ELSIF    msg.ch = Right    THEN    Move(F, 1)
  72.                         ELSIF    F.car > 0    THEN    (* caret set *)
  73.                             IF    msg.ch = LF    THEN    msg.ch := CR; TextFrames.Handle(F, msg)
  74.                             ELSIF    msg.ch = CR    THEN    NewLine(F)
  75.                             ELSE    TextFrames.Handle(F, msg)
  76.                             END
  77.                         END
  78.                     ELSE TextFrames.Handle(F, msg)
  79.                     END
  80.                 END
  81.             ELSIF    msg IS EdTMsg    THEN
  82.                 WITH    msg: EdTMsg    DO
  83.                     IF    (F.text = msg.text) & (F.sel = 0)    THEN    TextFrames.SetSelection(F, msg.beg, msg.end); F.time := msg.time    END
  84.                 END
  85.             ELSE    TextFrames.Handle(F, msg)
  86.             END
  87.         END
  88.     END Handle;
  89.     PROCEDURE Open*;
  90.         VAR S: Texts.Scanner; T: Texts.Text; V: MenuViewers.Viewer; x, y: INTEGER; beg, end, time: LONGINT;
  91.     BEGIN    Texts.OpenScanner(S, Oberon.Par.text, Oberon.Par.pos); Texts.Scan(S);
  92.         IF    (S.class = Texts.Char) & (S.c = "^") OR (S.line # 0)    THEN    Oberon.GetSelection(T, beg, end, time);
  93.             IF    time >= 0    THEN    Texts.OpenScanner(S, T, beg); Texts.Scan(S)    END
  94.         END;
  95.         IF    S.class = Texts.Name    THEN    Oberon.AllocateUserViewer(Oberon.Mouse.X, x, y);
  96.             V := MenuViewers.New(
  97.                 TextFrames.NewMenu(S.s, Menu),
  98.                 TextFrames.NewText(TextFrames.Text(S.s), 0),
  99.                 TextFrames.menuH, x, y);
  100.             V.dsc.next.handle := Handle
  101.         END
  102.     END Open;
  103. BEGIN
  104.     Texts.OpenWriter(W)
  105. END EdT.
  106.